export const formatFileTime = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('-') +
    '-' +
    [hour, minute, second].map(formatNumber).join('-') +
    '-' +
    `${new Date().getTime()}`.substring(10)
  )
}
export const formatNumber = (n) => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/*不另外处理1-9之间的数前缀加0 */
const formatNotZeroNumber = (n) => {
  n = n.toString()
  return n
}
/*获取 （年-月-日 : 时:分:秒） 的格式 */
export const formatTime = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('-') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}
/*获取 某一天的0点*/
export const getDayStart = (time) => {
  return yearMonthDay(time) + ' 00:00:00'
}
/*获取 某一天的12点*/
export const getDayEnd = (time) => {
  return yearMonthDay(time) + ' 23:59:59'
}
/*获取 （年-月-日） 的格式 */
export const yearMonthDay = (time) => {
  let date = new Date(time)
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate()
  d = d < 10 ? '0' + d : d
  // let h = date.getHours();
  // let minute = date.getMinutes();
  // minute = minute < 10 ? ('0' + minute) : minute;
  return y + '-' + m + '-' + d
}
/*获取一个月的天数 */
export const getCountDays = (time) => {
  let curDate = new Date(time)
  let curMonth = curDate.getMonth()
  curDate.setMonth(curMonth + 1)
  curDate.setDate(0)
  return curDate.getDate()
}

/*获取某月份的每一天*/
export const getEveryDay = (time) => {
  const dataArr = []
  let day = getCountDays(time)
  // 获取当前日期
  let date = new Date(time)
  // 获取当前月份
  let nowMonth = date.getMonth() + 1
  // 对月份进行处理，1-9月在前面添加一个“0”
  // if (nowMonth >= 1 && nowMonth <= 9) {
  // nowMonth = "0" + nowMonth;
  // }
  for (let k = 1; k <= day; k++) {
    // 对日期进行处理，1-9号在前面添加一个“0”
    // if (k >= 0 && k <= 9) {
    //   k = k;
    // }
    dataArr.push(`${nowMonth}.${k}`)
  }
  return dataArr
}
/*获取 （月.日） 的格式 */
export const WeekTime = (date) => {
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [month, day].map(formatNotZeroNumber).join('.')
}
// 获取某天所在的周的每一天
export const getWeekTime = (time) => {
  let dateString = yearMonthDay(new Date(time)) //当天的日期，例如2020-2-28
  let presentDate = new Date(dateString)
  let today = presentDate.getDay() !== 0 ? presentDate.getDay() : 7
  return Array.from(new Array(7), function (val, index) {
    return WeekTime(new Date(presentDate.getTime() - (today - index - 1) * 24 * 60 * 60 * 1000))
  })
}
//本周第一天
export const weekFirst = (date) => {
  let week = date.getDay() //星期
  if (week === 0) {
    week = 7
  }
  let weekFirstDay = new Date(date - (week - 1) * 86400000)
  return yearMonthDay(weekFirstDay)
}
// 本周最后一天
export const weekLast = (date) => {
  let week = date.getDay() //星期
  if (week === 0) {
    week = 7
  }
  var weekFirstDay = new Date(date - (week - 1) * 86400000)
  var weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000)
  return yearMonthDay(weekLastDay)
}
export function numberToChinese(num) {
  if (num === 1) {
    return '初'
  }
  const units = ['', '十', '百', '千', '万', '亿']
  const digits = '零一二三四五六七八九'.split('')

  if (num === 0) return digits[0]

  let chinese = ''
  let unitPos = 0
  let zeroFlag = false

  while (num > 0) {
    const digit = num % 10

    if (digit === 0) {
      if (!zeroFlag) {
        zeroFlag = true
        chinese = digits[digit] + chinese
      }
    } else {
      zeroFlag = false
      chinese = digits[digit] + units[unitPos] + chinese
    }

    num = Math.floor(num / 10)
    unitPos++
  }

  // 处理特殊情况
  if (chinese.startsWith('一十')) {
    chinese = chinese.slice(1)
  }

  return chinese
}
export function getColorFromChar(char) {
  // 将字符转换为其 Unicode 码
  const charCode = char.charCodeAt(0)

  // 使用字符的 Unicode 码生成 RGB 值
  const r = (charCode * 123) % 256 // 0-255 的红色通道
  const g = (charCode * 321) % 256 // 0-255 的绿色通道
  const b = (charCode * 213) % 256 // 0-255 的蓝色通道

  // 将每个颜色值转换为两位的16进制，不足的部分补0
  const redHex = r.toString(16).padStart(2, '0')
  const greenHex = g.toString(16).padStart(2, '0')
  const blueHex = b.toString(16).padStart(2, '0')

  // 拼接成完整的颜色代码
  return `#${redHex}${greenHex}${blueHex}`
}

export function debounce(fn, delay) {
  let timer = null //借助闭包
  return function () {
    if (timer) {
      clearTimeout(timer) //进入该分支语句，说明当前正在一个计时过程中，并且又触发了相同事件。所以要取消当前的计时，重新开始计时
      timer = setTimeout(fn, delay)
    } else {
      timer = setTimeout(fn, delay) // 进入该分支说明当前并没有在计时，那么就开始一个计时
    }
  }
}
